package com.meihuak.cloud_ordering.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
public class SendController {
    @ResponseBody
    @RequestMapping("/test")
    public static void test(){
        System.out.println("1111");
    }





    public static String getOpenid(@RequestParam(value="code",required=false)String code){
        //微信官方提供获取openid的url
        String WX_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        //替换appid，appsecret，和code
        String requestUrl = WX_URL.replace("APPID", "wx96c017c0e0ed66fc").//填写自己的appid
                replace("SECRET", "021cb8d5931eb8cf2e64c282e0ed52ff").replace("JSCODE", code).//填写自己的appsecret，
                replace("authorization_code", "authorization_code");
        //定义json对象
        JSONObject convertvalue=new JSONObject();
        //调用get方法
        String  returnvalue=GET(requestUrl);
        //解析返回值，对象解析成字符串
        convertvalue= (JSONObject) JSON.parse(returnvalue);
        String session_key= (String) convertvalue.get("session_key");
        //        System.out.println(returnvalue);
//        System.out.println("openid:"+openid);
//        System.out.println("session_key:"+session_key);
//        System.out.println("code"+code);

        return (String) convertvalue.get("openid");

    }

    public static String getSessionKey(@RequestParam(value="code",required=false)String code){
        //微信官方提供获取openid的url
        String WX_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        //替换appid，appsecret，和code
        String requestUrl = WX_URL.replace("APPID", "wxe58343dd6d26d23d").//填写自己的appid
                replace("SECRET", "d88579ea5e905dbf4e8225ca782db09d").replace("JSCODE", code).//填写自己的appsecret，
                replace("authorization_code", "authorization_code");
        //定义json对象
        JSONObject convertvalue=new JSONObject();
        //调用get方法
        String  returnvalue=GET(requestUrl);
        //解析返回值，对象解析成字符串
        convertvalue= (JSONObject) JSON.parse(returnvalue);

        return (String) convertvalue.get("session_key");

    }






    //发起ge请求---生成openid和session_key
    public static String GET(String url) {
        StringBuilder result= new StringBuilder();
        BufferedReader in = null;
        InputStream is = null;
        InputStreamReader isr = null;
        try{
            //构造url对象
            URL realUrl = new URL(url);
            //获取一个对应该URL的URLConnection对象
            URLConnection conn = realUrl.openConnection();
            conn.connect();
            is = conn.getInputStream();
            isr = new InputStreamReader(is);
            in = new BufferedReader(isr);
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("异常出现");
        }
        //关闭资源
        finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (is != null) {
                    is.close();
                }
                if (isr != null) {
                    isr.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("出现异常");
            }
        }
        return result.toString();
    }

}

